package xyz.yuzegod.megawalls.game;

import com.gmail.filoghost.holographicdisplays.api.Hologram;
import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Equipment;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import xyz.yuzegod.megawalls.MegaWalls;

import java.util.*;

public class FakePlayer
{
    public static Map<GamePlayer, FakePlayer> fakePlayerMap;
    public static List<GamePlayer> list;
    private GamePlayer gamePlayer;
    private boolean killed;
    private Location location;
    private List<ItemStack> drops;
    private NPC npc;
    private Hologram hologram;
    
    public FakePlayer(final GamePlayer gamePlayer) {
        this.killed = false;
        this.drops = new ArrayList<ItemStack>();
        this.gamePlayer = gamePlayer;
        final Player player = gamePlayer.getPlayer();
        this.location = player.getLocation().clone();
        for (final ItemStack itemStack : player.getInventory().getContents()) {
            if (itemStack != null) {
                if (itemStack.getType() != Material.AIR) {
                    this.drops.add(itemStack);
                }
            }
        }
        for (final ItemStack itemStack : player.getInventory().getArmorContents()) {
            if (itemStack != null) {
                if (itemStack.getType() != Material.AIR) {
                    this.drops.add(itemStack);
                }
            }
        }
        (this.hologram = HologramsAPI.createHologram((Plugin)MegaWalls.getInstance(), this.location.add(0.0, 3.0, 0.0))).appendTextLine(gamePlayer.getGameTeam().getTeamColor().getChatColor() + "§lLOGGED OUT");
        (this.npc = CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, gamePlayer.getUuid(), new Random().nextInt(100000), gamePlayer.getName())).setName(gamePlayer.getGameTeam().getTeamColor().getChatColor() + gamePlayer.getGameTeam().getTeamColor().getTag() + " " + gamePlayer.getName());
        this.npc.spawn(this.location);
        final Equipment trait = (Equipment)this.npc.getTrait((Class)Equipment.class);
        trait.set(Equipment.EquipmentSlot.HELMET, player.getInventory().getHelmet());
        trait.set(Equipment.EquipmentSlot.CHESTPLATE, player.getInventory().getChestplate());
        trait.set(Equipment.EquipmentSlot.LEGGINGS, player.getInventory().getLeggings());
        trait.set(Equipment.EquipmentSlot.BOOTS, player.getInventory().getBoots());
        trait.set(Equipment.EquipmentSlot.HAND, player.getItemInHand());
        FakePlayer.fakePlayerMap.put(gamePlayer, this);
        FakePlayer.list.add(gamePlayer);
        MegaWalls.updateRejoin(gamePlayer, MegaWalls.getInstance().getConfig().getString("servername"), System.currentTimeMillis() + 3600000L);
    }
    
    public static FakePlayer getFakePlayer(final NPC npc) {
        for (final FakePlayer fakePlayer : FakePlayer.fakePlayerMap.values()) {
            if (fakePlayer.getNpc().equals(npc)) {
                return fakePlayer;
            }
        }
        return null;
    }
    
    public void teleport(final Location location) {
        this.npc.teleport(location, PlayerTeleportEvent.TeleportCause.PLUGIN);
        this.hologram.teleport(location.add(0.0, 3.0, 0.0));
        this.location = location;
    }
    
    public void delete() {
        this.drops.clear();
        this.npc.destroy();
        this.hologram.delete();
        FakePlayer.fakePlayerMap.remove(this);
    }
    
    public GamePlayer getGamePlayer() {
        return this.gamePlayer;
    }
    
    public boolean isKilled() {
        return this.killed;
    }
    
    public void setKilled(final boolean killed) {
        this.killed = killed;
    }
    
    public Location getLocation() {
        return this.location;
    }
    
    public List<ItemStack> getDrops() {
        return this.drops;
    }
    
    public NPC getNpc() {
        return this.npc;
    }
    
    public Hologram getHologram() {
        return this.hologram;
    }
    
    static {
        FakePlayer.fakePlayerMap = new HashMap<GamePlayer, FakePlayer>();
        FakePlayer.list = new ArrayList<GamePlayer>();
    }
}
